package com.huifaapp.mall.selectImage;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/**
 * 正方形的Layout
 */
public class SquareLayout extends FrameLayout {
	public SquareLayout(Context context) {
		super(context);
	}

	public SquareLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		
		int result = 0;
		
		if(widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY){
			result = Math.min(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
		}else if(widthMode == MeasureSpec.EXACTLY){
			result = MeasureSpec.getSize(widthMeasureSpec);
		}else if(heightMode == MeasureSpec.EXACTLY){
			result = MeasureSpec.getSize(heightMeasureSpec);
		}
		if(result == 0){
			int width = resolveSize(MeasureSpec.getSize(widthMeasureSpec), widthMeasureSpec);
			int height = resolveSize(MeasureSpec.getSize(heightMeasureSpec), heightMeasureSpec);
			result = Math.min(width, height);
		}
		
		int newMeasureSpec = MeasureSpec.makeMeasureSpec(result, MeasureSpec.EXACTLY);
		
		super.onMeasure(newMeasureSpec, newMeasureSpec);
	}
}
